package com.zhouke.server_test;


import com.zhouke.server_test.common.Result;
import com.zhouke.server_test.model.UserDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.HashMap;

@Component
@Slf4j
public class ServerRunner implements ApplicationRunner {
    @Autowired
    private RestTemplate restTemplate;
    @Override
    public void run(ApplicationArguments args)throws Exception{
        login();
    }
    private void login(){
        UserDTO user=UserDTO.builder().username("admin").password("admin").build();
        //请求
        RequestEntity<UserDTO> request=RequestEntity
                .post(UriComponentsBuilder.fromUriString("http://certificate-server/user/login")
                        .build().toUri())
                .body(user);
        //返回
        ResponseEntity<Result> response=restTemplate.exchange(request,Result.class);
        HashMap ret=(HashMap) response.getBody().getData();
        log.info("======================token : {}",ret.get("token"));
    }
}
